#!/usr/bin/env python
# coding=UTF-8
'''
 * @Author       : z417
 * @Date         : 2020-03-10 03:28:09
 * @LastEditors: z417
 * @LastEditTime: 2020-03-10 03:59:28
 * @FilePath     : \\python/hrunDemo/debugtalk.py
 * @Description  : file desc
'''

import time
import hashlib

SECRET_KEY = 'autoClass'


def sleep(n_secs):
    time.sleep(n_secs)


def get_json(obj: dict):
    obj['ts'] = int(time.time())
    sign = get_sign(obj)
    obj['sign'] = sign
    return obj


def sort_req(req):
    s_key = sorted(req.keys(), reverse=False)
    if 'sign' in s_key:
        s_key.remove('sign')
    for k in s_key:
        if req[k] is None or not req[k]:
            s_key.remove(k)
    return s_key


def get_sign(req):
    bs = ''
    for i in sort_req(req):
        bs += '{}={}&'.format(i, req[i])
    bs += SECRET_KEY
    sign = hashlib.md5(bs.encode('utf-8')).hexdigest()
    return sign
